package com.zhe.zheblogmaster.service.impl;

import com.zhe.zheblogmaster.config.BlogConstants;
import com.zhe.zheblogmaster.entity.Config;
import com.zhe.zheblogmaster.mapper.ConfigMapper;
import com.zhe.zheblogmaster.service.ConfigService;
import com.zhe.zheblogmaster.util.ZheBlogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author WHAOU
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigMapper configMapper;

    @Override
    public Map<String, String> getAllConfig() {
        List<Config> configList = configMapper.selectAllConfig();
        if (configList.size() == 0) {
            return BlogConstants.CONFIG_MAP;
        }
        return configList.stream().collect(Collectors.toMap(Config::getConfigName, Config::getConfigValue));
    }

    @Override
    public int updateConfig(String configName, String configValue) {
        Config config = configMapper.selectByPrimaryKey(configName);
        if (config != null) {
            config.setConfigValue(configValue);
            config.setUpdateTime(ZheBlogUtils.getNow());
            return configMapper.updateByPrimaryKeySelective(config);
        }
        return 0;
    }
}
